﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;
using System.Web.Security;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;
using System.Collections;
using System.Drawing;

public partial class Teacher_TeacherUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DBaccess db = new DBaccess();
        SqlDataReader type = db.GetReader("select * from mr_course");
        DropDownList1.DataSource = type;
        DropDownList1.DataTextField = "cname";
        DropDownList1.DataBind();
        SetTop(0);
        DataSet ds = new DataSet();
        String sid = Session["mid"].ToString();
        ds = db.GetDataSet("select * from mr_v_comment where time > (select last_comment_time from mr_member where mid ='" + sid + "')");
        HyperLinkRecentComment.Text = "最新评论(" + ds.Tables[0].Rows.Count.ToString() + ")";
        HyperLinkRecentComment.NavigateUrl = "~/recentcomment.aspx";
        HyperLinkRecentComment.ForeColor = Color.Blue;
        ds = db.GetDataSet("select * from mr_v_download where time > (select last_download_time from mr_member where mid ='" + sid + "')");
        HyperLinkRecentDownload.Text = "最新下载(" + ds.Tables[0].Rows.Count.ToString() + ")";
        HyperLinkRecentDownload.NavigateUrl = "~/recentdownload.aspx";
        HyperLinkRecentDownload.ForeColor = Color.Blue;
    }
    public void SetTop(int tag)
    {
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        String tablename = "mr_test";
        String condition = "";
        DateTime currentTime = new DateTime();
        currentTime = DateTime.Now;
        if (tag == 0)
        {
            tablename = "mr_v_downloadcount";
            LabelTitle.Text = "总排行榜";
        }
        else if (tag == 1)
        {
            tablename = "mr_v_downloadcount_month ";
            condition = "where year = '" + currentTime.Year.ToString() + "' and month = '" + (currentTime.Month - 1).ToString() + "'";
            LabelTitle.Text = "月排行榜";
        }
        else if (tag == 2)
        {
            tablename = "mr_v_downloadcount_year ";
            condition = "where year = '" + currentTime.Year.ToString() + "'";
            LabelTitle.Text = "年排行榜";
        }
        ds = db.GetDataSet("select top 5 * from " + tablename + condition + "  order by num desc");
        HyperLinkRank1.Text = ds.Tables[0].Rows[0]["rname"].ToString();
        HyperLinkRank1.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount1.Text = ds.Tables[0].Rows[0]["num"].ToString();
        HyperLinkRank2.Text = ds.Tables[0].Rows[1]["rname"].ToString();
        HyperLinkRank2.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount2.Text = ds.Tables[0].Rows[1]["num"].ToString();
        HyperLinkRank3.Text = ds.Tables[0].Rows[2]["rname"].ToString();
        HyperLinkRank3.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount3.Text = ds.Tables[0].Rows[2]["num"].ToString();
        HyperLinkRank4.Text = ds.Tables[0].Rows[3]["rname"].ToString();
        HyperLinkRank4.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount4.Text = ds.Tables[0].Rows[3]["num"].ToString();
        HyperLinkRank5.Text = ds.Tables[0].Rows[4]["rname"].ToString();
        HyperLinkRank5.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount5.Text = ds.Tables[0].Rows[4]["num"].ToString();
    }
    protected void ButtonMonth_Click(object sender, EventArgs e)
    {
        SetTop(1);
    }
    protected void ButtonYear_Click(object sender, EventArgs e)
    {
        SetTop(2);
    }
    protected void ButtonAll_Click1(object sender, EventArgs e)
    {
        SetTop(0);
    }
    protected void Button1_Click2(object sender, EventArgs e)
    {
        string type = DropDownList1.Text;
        string title = TextBox1.Text;
        string keyword = TextBox5.Text;
        string introduction = TextBox2.Text;
        string score = TextBox3.Text;
        int cid = 0;
        int id = 0;
        int flag = 0;
        string value="";
        int temp = DropDownList1.SelectedIndex;
        switch (temp)
        { 
            case 0:
                value = "操作系统";
                break;
            case 1:
                value = "网络与通信";
                break;
            case 2:
                value = "环境化学";
                break;
            case 3:
                value = "机械制造基础";
                break;
            case 4:
                value = "建筑概念设计与造型";
                break;
            case 5:
                value = "大学语文";
                break;
            case 6:
                value = "美术学学科资源导航";
                break;
            case 7:
                value = "解读电影";
                break;
            case 8:
                value = "马克思主义";
                break;
            case 9:
                value = "大学物理";
                break;
            case 10:
                value = "数据库原理";
                break;
            case 11:
                value = "电子商务";
                break;
        }
        if (TextBox1.Text == "")
        {
            Response.Write("<script>alert('文件名不能为空!');</script>");
        }
        else if (!FileUpload1.HasFile)
        {
            Response.Write("<script>alert('请你上传文件!');</script>");
        }
        else
        {
            string kzm = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            //string path = FileUpload1.PostedFile.FileName;
            string path = "updata\\" + value + "\\" + TextBox1.Text + kzm;
            string fullpath = MapPath("..") + "\\" + path;
            string resourcepath="~updata\\"+ value + "\\" + TextBox1.Text + kzm;
            FileUpload1.PostedFile.SaveAs(fullpath);
            string kzm1 = System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();
            //string path = FileUpload1.PostedFile.FileName;

            string time = DateTime.Now.ToString();
            string time1 = DateTime.Now.ToFileTime().ToString();
            string picture = title + time1;
            string path1 = "Picture\\" + picture + kzm1;
            string fullpath1 = MapPath("..") + "\\" + path1;
            string resourcepath1 = "~\\Picture\\"  + picture + kzm1;
            FileUpload1.PostedFile.SaveAs(fullpath1);
            DBaccess db = new DBaccess();
            SqlDataReader num = db.GetReader("select count(*) from mr_resource");
            num.Read();
            if ((int)num.GetValue(0) !=0)
            {
                id = (int)num.GetValue(0) + 1;
            }
            else
                id = 1;
            SqlDataReader name = db.GetReader("select rname from mr_resource");
            while (name.Read())
            {
                string temp1 = (string)name["rname"];
                if (title == temp1)
                {
                    Response.Write("<script>alert('数据库中已有相同名字的文件，请修改文件名!');</script>");
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                    flag = 1;
                    break;
                }
            }
            System.Drawing.Image image = System.Drawing.Image.FromStream(FileUpload2.PostedFile.InputStream);

            int height = image.Height;
            int width = image.Width;
            if (height == 64 && width == 64)
            {
                if (flag == 0)
                {
                    cid = DropDownList1.SelectedIndex + 1;
                    string sql = "insert into mr_resource(rid,cid,gid,owner,rname,keyword,introduction,score,resource,logo,upload_time) values('" + id + "','" + cid + "','1','" + Session["mid"].ToString() + "','" + title + "','" + keyword + "','"+introduction+"','" + score + "','" + resourcepath + "','" + resourcepath1 + "','"+time+"')";//数据库操作
                    if (db.ExecSql(sql) != 0)
                    {
                        Response.Write("<script>alert('上传成功!');</script>");
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        TextBox5.Text = "";
                        TextBox3.Text = "";
                    }
                    else
                    {
                        Response.Write("<script>alert('上传出错!');</script>");
                    }
                }
            }
            else
                Response.Write("<script>alert('请上传大小为64*64大小的图片');</script>");
        }
    }
    
   
    protected void Button1_Click3(object sender, EventArgs e)
    {
        Response.Redirect("./TeacherUpdate.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["search"] = TextBox4.Text;
        Response.Redirect("./test.aspx");
    }
}